var table;

$(function () {
    loadDeviceList();
    // loadTable();
    hideModal();
});

function loadOption(data) {
    $('#dealer').select2({
        data: data,
        minimumResultsForSearch: Infinity
    });
}

/**
 * 加载设备列表
 */
function loadDeviceList() {
    var url = baseDeviceUrl + "list/device?start=0&limit=1000000";
    ajaxCommon.request('get',url,{},function (data) {
        var array = [];
        if(data.data) {
            array = data.data.rows
            loadTable(array);
        }else{
            var obj={
                uid: '',
                name: '',
                created: ''
            }
            array.push(obj)
            loadTable(array,true);
        }
    })
}

function loadTable(data,type) {
    if(type){
        var obj = {
            data: data,
            columns: [
                {data: 'uid'},
                {data: 'name'},
                {
                    data: 'created',
                    render: function (data, type, row, meta) {
                        return formatDate1(row.created);
                    }
                }
            ]
        }
    }else{
        var obj = {
            data: data,
            columns: [
                {data: 'uid'},
                {data: 'name'},
                {
                    data: 'created',
                    render: function (data, type, row, meta) {
                        return formatDate1(row.created);
                    }
                }
            ],
            columnDefs: [{
                targets: 3,
                render: function (data, type, row, meta) {
                    return "<button type='button' style='margin-right: 10px;border: 1px solid #0D1113;color: #0D1113;background: transparent;' class='btn btn-primary' onclick='editDevice(" + JSON.stringify(row) + ");'>编辑</button>";
                }
            }]
        }
    }
    ajaxCommon.loadTableData('#deviceList',obj)
}

var currDevice;

/**
 * 编辑按钮点击事件
 * @param equipment
 */
function editDevice(equipment) {
    currDevice = equipment;
    var url = baseDealerUrl + "list/dealer?start=0&limit=10000";
    ajaxCommon.request('get',url,{},function (data) {
        var stores = [];
        for (var i = 0; i < data.data.rows.length; i++) {
            var store = {"id": data.data.rows[i].dealerCode, "text": data.data.rows[i].dealerName};
            stores.push(store);
        }
        loadOption(stores);
        $('#editModal').modal('toggle');
    })
}

/**
 * 模态框隐藏时的事件
 */
function hideModal() {
    $("#editModal").on("hide.bs.modal", function () {
        $("#dealer").children().remove();
    });
}

/**
 * 提交按钮点击事件
 */
function submitEdit() {
    var dealer = $("#dealer").select2("data")[0];
    if (!isNull(currDevice) && !isNull(dealer)) {
        var url = baseDeviceUrl + "update/device";
        ajaxCommon.request('post',url,JSON.stringify({"uid": currDevice.uid, "dealerCode": dealer.id}),function (data) {
            if (data.data) {
                $('#editModal').modal('hide');
                loadDeviceList();
                // table.ajax.reload();
            } else {
                swal("失败");
                $('#editModal').modal('hide');
            }
        })
    }
}
